/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sxu.shadowdrawabledemo.slice;

import com.sxu.shadowdrawable.ShadowDrawable;
import com.sxu.shadowdrawabledemo.ExampleContainerAbility;
import com.sxu.shadowdrawabledemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ScrollView;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.agp.window.service.DisplayManager;
/**
 * MainAbilitySlice类
 * * @since 2021-04-07
 */
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private Text textView1;
    private Text textView2;
    private Text textView3;
    private Text textView4;
    private Button gridExampleText;
    private Button listExampleText;
    private Button recycleExampleText;
    private Button pagerExampleText;
    private static final int SHAPERADIUS = 8;
    private static final int SHADOWRADIUS = 6;
    private static final int TEXT3SHADOWRADIUS = 10;
    private static final int OFFSETX = 3;
    private static final int OFFSETY = 3;
    private int CONTAINERTYPE = -1;
    private static final int SCROLLBARTHICKNESS = 12;
    private static final int DIRECEION = 1;
    private static final float DENSITYFLOAT = 0.5f;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        textView1 = (Text) findComponentById(ResourceTable.Id_text1);
        textView2 = (Text) findComponentById(ResourceTable.Id_text2);
        textView3 = (Text) findComponentById(ResourceTable.Id_text3);
        textView4 = (Text) findComponentById(ResourceTable.Id_text4);
        ScrollView mainscroll = (ScrollView) findComponentById(ResourceTable.Id_main_scroll);
        mainscroll.enableScrollBar(DIRECEION,true);
        mainscroll.setScrollbarThickness(SCROLLBARTHICKNESS);
        mainscroll.setScrollbarColor(Color.GRAY);

        gridExampleText = (Button) findComponentById(ResourceTable.Id_grid_example_text);
        listExampleText = (Button) findComponentById(ResourceTable.Id_list_example_text);
        recycleExampleText = (Button) findComponentById(ResourceTable.Id_recycle_example_text);
        pagerExampleText = (Button) findComponentById(ResourceTable.Id_view_page_example_text);

        listExampleText.setClickedListener(this);
        gridExampleText.setClickedListener(this::onClick);
        recycleExampleText.setClickedListener(this::onClick);
        pagerExampleText.setClickedListener(this::onClick);

        ShadowDrawable.setShadowDrawable(textView1,Color.getIntColor("#2979FF"),dpToPx(SHAPERADIUS),
                Color.getIntColor("#992979FF"),dpToPx(SHADOWRADIUS),0,0);

        ShadowDrawable.setShadowDrawable(textView2, new int[] {
                        Color.getIntColor("#536DFE"), Color.getIntColor("#7C4DFF")}, dpToPx(SHAPERADIUS),
                Color.getIntColor("#997C4DFF"), dpToPx(SHADOWRADIUS), dpToPx(OFFSETX), dpToPx(OFFSETY));

        ShadowDrawable.setShadowDrawable(textView3,ShadowDrawable.SHAPE_CIRCLE,Color.getIntColor("#2979FF"),
                0,Color.getIntColor("#aa536DFE"),dpToPx(TEXT3SHADOWRADIUS),0,0);

        ShadowDrawable.setShadowDrawable(textView4,ShadowDrawable.SHAPE_CIRCLE,Color.getIntColor("#7C4DFF"),
                dpToPx(SHAPERADIUS),Color.getIntColor("#992979FF"),
                dpToPx(SHADOWRADIUS),dpToPx(OFFSETX),dpToPx(OFFSETY));
    }

    private int dpToPx(int dp) {
        float density = DisplayManager.getInstance().getDefaultDisplay(this).get().getAttributes().densityPixels;
        return (int) (dp * density + DENSITYFLOAT);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_grid_example_text:
                CONTAINERTYPE = ExampleContainerAbility.CONTAINER_TYPE_GRID;

                break;
            case ResourceTable.Id_list_example_text:
                CONTAINERTYPE = ExampleContainerAbility.CONTAINER_TYPE_LIST;

                break;
            case ResourceTable.Id_recycle_example_text:
                CONTAINERTYPE = ExampleContainerAbility.CONTAINER_TYPE_RECYCLE;

                break;
            case ResourceTable.Id_view_page_example_text:
                CONTAINERTYPE = ExampleContainerAbility.CONTAINER_TYPE_PAGER;
                break;
            default:
                break;
        }
        if (CONTAINERTYPE != -1) {
            ExampleContainerAbility.enter(this, CONTAINERTYPE);
        }
    }
}
